package com.zuo.interceptor;

import com.zuo.entity.User;
import com.zuo.util.HrmConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CommonInterceptor  extends HandlerInterceptorAdapter {

        private final Logger log = LoggerFactory.getLogger(CommonInterceptor.class);

        @Override  
        public boolean preHandle(HttpServletRequest request,
                                 HttpServletResponse response, Object handler) throws Exception {
            String requestUri = request.getRequestURI();
            String contextPath = request.getContextPath();
            String url = requestUri.substring(contextPath.length());
//            System.out.println("url = " + url);
            //可以过
            if ("/toLoginForm.action".equals(url)||"/login.action".equals(url)||"/toFace.action".equals(url)||"/user/faceLogin.action".equals(url)) {
                    return true;
            }else {//不能过
                User user =  (User)request.getSession().getAttribute(HrmConstants.USER_SESSION);
                    if(user == null){
                            request.getRequestDispatcher("toLoginForm.action").forward(request, response);
                            return false;
                    }else
                            return true;   
           }
        }

        @Override  
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                               ModelAndView modelAndView) throws Exception {
        }

        @Override  
        public void afterCompletion(HttpServletRequest request,
                                    HttpServletResponse response, Object handler, Exception ex) throws Exception {
        }  
}